package view.user;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import controller.KeyHandler;
import controller.ActionEvents.ActionHandler;


public class Login extends JPanel {

	/** Declares the class variables */
	private JLabel lblBigHeader; // First header
	private JLabel lblSmallHeader; // Second header
	private JTextField txtUser; // Textfield in which the user types his ID
	private JPasswordField passwordField; //field with hidden characters where the user types his password
	private JButton btnLogin; // JButton which passes the entered information to ActListener.java
	
	private ActionHandler actionHandler; // Creates an object of ActListener.java
	private JButton btnAdminLogin;
	
	
	/**
	 * Create the panel.
	 */
	public Login( ActionHandler actionHandler) 
	{
		
		setLayout(null);
		setPreferredSize(new Dimension(750, 480));
		this.actionHandler = actionHandler;

		setBackground(Color.WHITE);
		

		lblBigHeader = new JLabel("Carbon Credits System");
		lblBigHeader.setFont(new Font("Euphemia UCAS", Font.PLAIN, 28));
		lblBigHeader.setBounds(239, 68, 307, 53);
		add(lblBigHeader); // Ads the label to JPanel.
		
		
		lblSmallHeader = new JLabel("Welcome");
		lblSmallHeader.setFont(new Font("Euphemia UCAS", Font.PLAIN, 14));
		lblSmallHeader.setBounds(354, 131, 61, 16);
		add(lblSmallHeader); // Ads the label to JPanel.
		
		txtUser = new JTextField();
		txtUser.setText("170291");
		txtUser.setColumns(10);
		txtUser.setBounds(320, 159, 142, 28);
		txtUser.setBackground(Color.decode("#f1f1f1"));
		add(txtUser); // Ads the textfield to JPanel.
		

		btnLogin = new JButton("");
//		btnLogin.setIcon(new ImageIcon(Login.class.getResource("/Pictures/ux/submit.png")));
		btnLogin.setBounds(320, 221, 142, 47);
		btnLogin.setActionCommand("showLogin");
		btnLogin.addActionListener(actionHandler);
		btnLogin.setOpaque(false);
		btnLogin.setBorderPainted(false);
		btnLogin.setContentAreaFilled(false);
		add(btnLogin); // Ads the button to JPanel.
		
		
		
		passwordField = new JPasswordField();
		passwordField.setText("hej");
		passwordField.setToolTipText("Password");
		passwordField.setBounds(320, 192, 142, 28);
		passwordField.setBackground(Color.decode("#f1f1f1"));
		KeyListener kl;
		passwordField.addKeyListener(kl = new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {}
			@Override
			
			public void keyReleased(KeyEvent e) {
				int key = e.getKeyCode(); // gets the keycode from KeyEvent
				if(key == KeyEvent.VK_ENTER) // if keycode equals 10, run statement
				{
					btnLogin.doClick(); // Simulates a click on the JButton btnLogin
					txtUser.requestFocusInWindow(); /* Sets the focus to the textfield. If this werent done, the keylistener would
					loop infinite as long as the user tried to use the ENTER key to press the OptionPane. */
				}
			}
			@Override
			public void keyTyped(KeyEvent e) {}
			
		});
		add(passwordField); // Ads the password field to JPanel.
		


		
		btnAdminLogin = new JButton("");
//		btnAdminLogin.setIcon(new ImageIcon(Login.class.getResource("/Pictures/ux/aminLoginSubmit.png")));
		btnAdminLogin.addActionListener(actionHandler);
		btnAdminLogin.setActionCommand("showAdminLoginScreen");
		btnAdminLogin.setBounds(17, 420, 110, 39);
		btnAdminLogin.setBorderPainted(false);
		btnAdminLogin.setContentAreaFilled(false);
		add(btnAdminLogin);
		System.out.println("Login has runned");
	
	}
	/**
	 * @return the txtUser
	 */
	public JTextField getTxtUser() {
		return txtUser;
	}

	/**
	 * @param txtUser the txtUser to set
	 */
	public void setTxtUser(JTextField txtUser) {
		this.txtUser = txtUser;
	}

	/**
	 * @return the passwordField
	 */
	public JPasswordField getPasswordField() {
		return passwordField;
	}

	/**
	 * @param passwordField the passwordField to set
	 */
	public void setPasswordField(JPasswordField passwordField) {
		this.passwordField = passwordField;
	}

	/**
	 * @return the btnLogin
	 */
	public JButton getBtnLogin() {
		return btnLogin;
	}

	/**
	 * @return the btnAdminLogin
	 */
	public JButton getBtnAdminLogin() {
		return btnAdminLogin;
	}

	
} // End class Login.java
